<template>
	<view>
		<view class="status_bar"></view>
		<uni-notice-bar show-icon scrollable show-get-more @getmore="toNotice" :text="notice" />
		<view v-for="(item, index) in conversationList" :key="index" @click="toChat(item.conversationID, item.userProfile.userID)">
			<view class="item">
				<uni-badge class="left" :text="item.unreadCount" absolute="rightTop" size="small">
					<image :src="item.userProfile.avatar" mode="aspectFill"></image>
				</uni-badge>
				<view class="content">
					<view class="top">
						<view class="title">{{item.userProfile.nick}}</view>
						<view class="time">{{caculateTimeago(item.lastMessage.lastTime * 1000)}}</view>
					</view>
					<view class="sub-title">{{item.lastMessage.messageForShow}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {TUIConversationService} from "@tencentcloud/chat-uikit-engine";
	import constant from '@/common/constant.js'
    import IMUtil from "@/main/common/IMUtil.js";
	export default {
		data() {
			return {
				conversationList: [],
				notice: ''
			};
		},
		onLoad() {
			this.requestNotice();
		},
		onShow() {
			this.getConversationList();
		},
		methods: {
			async requestNotice(){
				let res = await this.$common.post("/app/message/list");
				if(0 == res.code){
					res.data.list.forEach(item=>{
						this.notice += item.title + '\t';
					})
				}
			},
			// 获取全量的会话列表
			getConversationList(){
				uni.$TUIKit.getConversationList().then(res => {
					//处理小秘书
					let xiaomishu = {};
					let hasXiaomishu = false;
					this.conversationList = []
					res.data.conversationList.forEach((item, index) => {
						 // || item.lastMessage.lastTime==0
						if('administrator'==item.userProfile.userID){
							item.userProfile.nick = "小秘书";
							item.userProfile.avatar = this.getImageUrl('logo.png');
							xiaomishu = item;
							res.data.conversationList.splice(index,1);
							hasXiaomishu = true;
						} else if(item.lastMessage.lastTime==0){
							item.lastMessage.lastTime = new Date().getTime() / 1000;
						}
						// else{
						// 	this.conversationList.push(item)
						// }
					})
					if(hasXiaomishu){
						res.data.conversationList.unshift(xiaomishu);
					}
					
					this.conversationList = res.data.conversationList;
					console.log("this.conversationList: " + JSON.stringify(this.conversationList.length));
				}).catch(error => {
				  console.warn('获取会话列表失败:', error);
				});
			},
			toChat(conversationID, texcode){
				IMUtil.toChat(texcode)
			},
			toNotice(){
				uni.navigateTo({
					url:'/main/notice/list/list'
				})
			},
			caculateTimeago(dateTimeStamp) {
				if(dateTimeStamp==0){
					const date = new Date();
					const year = date.getFullYear();
					const month = date.getMonth() + 1;
					const day = date.getDate();
					let result = year + '-' + month + '-' + day;
					if(result == '1970-01-01'){
						result = "刚刚";
						uni.setStorageSync('messageTime', dateTimeStamp)
						
					}
					return
				}
				
				
				const minute = 1000 * 60; // 把分，时，天，周，半个月，一个月用毫秒表示
				const hour = minute * 60;
				const day = hour * 24;
				const week = day * 7;
				const now = new Date().getTime(); // 获取当前时间毫秒
				const diffValue = now - dateTimeStamp; // 时间差
				let result = "";
				if (diffValue < 0) {
					return;
				}
				const minC = diffValue / minute; // 计算时间差的分，时，天，周，月
				const hourC = diffValue / hour;
				const dayC = diffValue / day;
				const weekC = diffValue / week;
				if (weekC >= 1 && weekC <= 4) {
					result = ` ${parseInt(weekC, 10)}周前`;
				} else if (dayC >= 1 && dayC <= 6) {
					result = ` ${parseInt(dayC, 10)}天前`;
				} else if (hourC >= 1 && hourC <= 23) {
					result = ` ${parseInt(hourC, 10)}小时前`;
				} else if (minC >= 1 && minC <= 59) {
					result = ` ${parseInt(minC, 10)}分钟前`;
				} else if (diffValue >= 0 && diffValue <= minute) {
					result = "刚刚";
				} else {
					// const datetime = new Date();
					// datetime.setTime(dateTimeStamp);
					// const Nyear = datetime.getFullYear();
					// const Nmonth =
					// 	datetime.getMonth() + 1 < 10 ?
					// 	`0${datetime.getMonth() + 1}` :
					// 	datetime.getMonth() + 1;
					// const Ndate =
					// 	datetime.getDate() < 10 ?
					// 	`0${datetime.getDate()}` :
					// 	datetime.getDate();
					// result = `${Nyear}-${Nmonth}-${Ndate}`;
					const date = new Date(dateTimeStamp);
					const year = date.getFullYear();
					const month = date.getMonth() + 1;
					const day = date.getDate();
					result = year + '-' + month + '-' + day;
					if(result == '1970-01-01'){
						result = "刚刚";
						uni.setStorageSync('messageTime', dateTimeStamp)
						
					}
				}
				return result;
			}
		}
	}
</script>

<style lang="scss">
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}
	
	.item{
		display: flex;
		flex-direction: row;
		width: 100%;
		box-sizing: border-box;
		padding: 20rpx;
		background-color: #fff;
		margin-bottom: 0.5px;
		
		.left{
			position: relative;
			width: 100rpx;
			height: 100rpx;
			margin-right: 20rpx;
			image{
				width: 100rpx;
				height: 100rpx;
				border-radius: 10rpx;
			}
		}
		
		.content{
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: center;
			
			.top{
				display: flex;
				flex-direction: row;
				.title{
					flex: 1;
					font-size: 36rpx;
					color: #222;
				}
				.time{
					font-size: 20rpx;
					color: #888;
				}
			}
			
			.sub-title{
				font-size: 28rpx;
				color: #555;
				margin-top: 10rpx;
			}
			
		}
		
	}
</style>

